package com.example.controller;


import com.example.dao.DocumentVideoTypeCrudRepository;
import com.example.pojo.PageBean;
import com.example.pojo.DocumentVideoType;
import com.example.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/documentVideoType")
@Slf4j
public class DocumentVideoTypeController {
    @Autowired
    private DocumentVideoTypeCrudRepository documentVideoTypeCrudRepository;
   

    @PostMapping("/addDocumentVideoType")
    public Result add(@RequestBody DocumentVideoType documentVideoType){
        DocumentVideoType save = documentVideoTypeCrudRepository.save(documentVideoType);
        return Result.success(save);
    }
    @DeleteMapping("/deleteDocumentVideoType")
    public Result delete(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            documentVideoTypeCrudRepository.deleteById(id);
        }
        return Result.success("删除成功");
    }
    @PostMapping("/updateDocumentVideoType")
    public Result update(@RequestBody DocumentVideoType documentVideoType){
        DocumentVideoType save = documentVideoTypeCrudRepository.save(documentVideoType);
        return Result.success(save);
    }
    @PostMapping("/getAllDocumentVideoType")
    public Result getAllDocumentVideoType(@RequestBody DocumentVideoType documentVideoType,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("documentVideoType:{}",documentVideoType);
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.Direction.ASC, "id");

        Page<DocumentVideoType> coursePage = documentVideoTypeCrudRepository.findAll(new Specification<DocumentVideoType>() {
            @Override
            public Predicate toPredicate(Root<DocumentVideoType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //获取条件对象
                Predicate predicate = cb.conjunction();
                //判断用户对象
                if (documentVideoType != null) {
                    // 根据 student 的属性添加查询条件
                    if (documentVideoType.getName() != null) {
                        predicate.getExpressions().add(cb.like(root.get("name"), "%" + documentVideoType.getName() + "%"));
                    }
                    if (documentVideoType.getId() != null) {
                        predicate.getExpressions().add(cb.equal(root.get("id"), documentVideoType.getId()));
                    }
                }
                return predicate;
            }
        }, pageable);
        List<DocumentVideoType> DocumentVideoTypes = coursePage.getContent();
        long elements = coursePage.getTotalElements();
        return Result.success(new PageBean(elements, DocumentVideoTypes));
    }

    @GetMapping("/getDocumentVideoTypeList")
    public Result getDocumentVideoTypeList(){
        Iterable<DocumentVideoType> all = documentVideoTypeCrudRepository.findAll();
        List<DocumentVideoType> DocumentVideoTypeList = new ArrayList<>();
        for (DocumentVideoType documentVideoType : all) {
            DocumentVideoTypeList.add(documentVideoType);
        }
        return Result.success(DocumentVideoTypeList);
    }

}
